package com.hz.activity7;

import lombok.extern.slf4j.Slf4j;
import org.activiti.engine.*;
import org.activiti.engine.impl.util.CollectionUtil;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.*;

@Slf4j
@SpringBootTest
public class TaskListener {

    @Autowired
    private TaskService taskService;
    @Autowired
    private RuntimeService runtimeService;

    /**
     * 部署流程
     */
    @Test
    public void deployListenerDefinition() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 获取流程定义与部署相关Service
        Deployment deployment = processEngine.getRepositoryService()
                // 创建一个部署对象
                .createDeployment()
                .name("任务监听器")
                // 加载资源文件
                .addClasspathResource("activity/listener.bpmn")
                // 完成部署
                .deploy();
        log.info("流程部署id===》"+ deployment.getId());
        log.info("流程部署name===》"+deployment.getName());
    }

    @Test
    public void startProcess() {
        Map<String, Object> variables = new HashMap<>();
        variables.put("applyUserId", "user123456");
        String processDefinitionKey = "task-listener";
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
        // 输出当前任务列表
        String id = processInstance.getId();
        log.info("数据ID==>: " + id);
        this.printTaskList(id);
    }

    @Test
    public void taskQuery() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<Task> list = processEngine.getTaskService().createTaskQuery()
                // 筛选 multiMeetingApprove 流程
                .processDefinitionKey("task-listener")
                .list();
        list.forEach(task -> {
            log.info("流程实例id:{}, 任务处理人:{}, 任务名:{}, 任务定义Key:{}, 任务id:{}",
                    task.getProcessInstanceId(),
                    task.getAssignee(),
                    task.getName(),
                    task.getTaskDefinitionKey(),
                    task.getId());
        });
        if (list.isEmpty()) {
            log.info("没有代办任务了，流程结束");
        }
    }

    @Test
    public void doCheckRejectTask() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = processEngine.getTaskService();
        List<Task> list = taskService.createTaskQuery().processDefinitionKey("task-listener").list();
        if(!CollectionUtil.isEmpty(list)){
            list.forEach(task->{
                //完成当前节点的任务id
                System.out.println("任务 id ===》"+task.getId());
                taskService.complete(task.getId());
            });
        }
        taskQuery();
    }

    private void printTaskList(String processInstanceId) {
        // 输出当前任务列表
        taskService.createTaskQuery().processInstanceId(processInstanceId).orderByTaskCreateTime().asc().list().forEach(k -> {
            System.out.println("===================任务列表===================");
            System.out.println("任务ID = " + k.getId());
            System.out.println("任务名称 = " + k.getName());
            System.out.println("任务负责人 = " + k.getAssignee());
            System.out.println("任务创建时间 = " + k.getCreateTime());

            System.out.println("===================身份列表===================");
            // 输出用户身份关系列表
            taskService.getIdentityLinksForTask(k.getId()).forEach(link -> {
                System.out.println("link.getType() = " + link.getType());
                System.out.println("link.getUserId() = " + link.getUserId());
                System.out.println("link.getGroupId() = " + link.getGroupId());
                System.out.println("link.getTaskId() = " + link.getTaskId());
            });
        });
    }

}
